﻿using System.Data;
using Channelvn.Cached;
using Core.Library;

namespace DataObject.QA
{
    public class QACache
    {
        public static bool SendQA_TimeExpire(int memberId)
        {
            var cacheName = "SendQA_TimeExpire" + memberId;
            var fromcache = CacheController.GetCache(Constants.DATA_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return false;
            }
            else
            {
                CacheController.Add(Constants.DATA_CACHE_CATEGORY, cacheName, true, Constants._30second);
                return true;
            }
        }

        public static DataTable QA_GetQAHomepage(int top)
        {
            var listParams = new object[] { top };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("QA_GetQAHomepage", listParams);
            var fromcache = CacheController.GetCache(Constants.QA_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return (DataTable)fromcache;
            }
            else
            {
                using (var dt = QAController.QA_GetQAHomepage(top))
                {
                    CacheController.Add(Constants.QA_CACHE_CATEGORY, cacheName, dt, Constants._15min);
                    return dt;
                }
            }
        }

        public static DataTable QA_GetListPaging(bool isFeature, int pageIndex, int pageSize)
        {
            var listParams = new object[] { isFeature, pageIndex, pageSize };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("QA_GetListPaging", listParams);
            var fromcache = CacheController.GetCache(Constants.QA_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return (DataTable)fromcache;
            }
            else
            {
                using (var dt = QAController.QA_GetListPaging(isFeature, pageIndex, pageSize))
                {
                    CacheController.Add(Constants.QA_CACHE_CATEGORY, cacheName, dt, Constants._15min);
                    return dt;
                }
            }
        }

        public static int QA_Count(bool isFeature)
        {
            var listParams = new object[] { isFeature };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("QA_Count", listParams);
            var fromcache = CacheController.GetCache(Constants.QA_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return (int)fromcache;
            }
            else
            {
                int r = QAController.QA_Count(isFeature);
                CacheController.Add(Constants.QA_CACHE_CATEGORY, cacheName, r, Constants._15min);
                return r;
            }
        }
    }
}
